﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Finance.Web.Models.Accounting
{
    public class DepositAccounting
    {
        protected int GetSpan(int? frequency)
        {
            int freq = frequency ?? 12;
            return 360 / freq;
        }

        protected double GetPercent(FinanceContext context, Deposit deposit, double amount)
        {
            var percent = 0.0;
            if (deposit.PercentAmountDepend != null && deposit.PercentAmountDepend.Value)
            {
                var depend = deposit.PercentAmountDepends.FirstOrDefault(i => amount >= i.MinAmount && (amount < i.MaxAmount || i.MaxAmount == null));
                if (depend != null && depend.Percent != null)
                {
                    percent = depend.Percent.Value;
                }
            }
            else if (deposit.Percent != null)
            {
                percent = deposit.Percent.Value;
            }

            return percent / 100;
        }

        protected double GetPercent(FinanceContext context, Deposit deposit, int period)
        {
            var percent = 0.0;
            if (deposit.HasPenalties != null && deposit.HasPenalties.Value)
            {
                var depend = deposit.PercentPenalties.FirstOrDefault(i => period >= i.From && (period < i.To || i.To == null));
                if (depend != null && depend.Percent != null)
                {
                    percent = depend.Percent.Value;
                }
            }
            else if (deposit.Percent != null)
            {
                percent = deposit.Percent.Value;
            }

            return percent / 100;
        }
    }
}